import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.io.*;
import java.util.List;

public class DOM4J_ {
    @Test
    public void loadXML() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/school.xml"));
        System.out.println(document);
    }
    @Test
    public void Retrieve() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/school.xml"));
        Element root = document.getRootElement();
        Element students = root.element("students");
        List<Element> student = students.elements("student");
        for (Element element : student) {
            System.out.println(element.element("name").getText() + " " + element.element("age").getText()
                    + " " + element.element("gender").getText());
        }
    }
    @Test
    public void Create() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/school.xml"));
        Element root = document.getRootElement();

        Element student = DocumentHelper.createElement("student");
        student.addAttribute("id", "3");

        Element name = DocumentHelper.createElement("name");
        name.setText("tom");
        Element age = DocumentHelper.createElement("age");
        age.setText("10");
        Element gender = DocumentHelper.createElement("gender");
        gender.setText("male");

        student.add(name);
        student.add(age);
        student.add(gender);

        root.element("students").add(student);

        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");

        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/school.xml"), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
    @Test
    public void Update() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/school.xml"));
        Element root = document.getRootElement();
        Element students = root.element("students");
        List<Element> student = students.elements("student");
        for (Element element : student) {
            Element age = element.element("age");
            age.setText(Integer.parseInt(age.getText()) + 5 + "");
        }
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/school.xml"), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
    @Test
    public void Delete() throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("src/school.xml"));
        Element root = document.getRootElement();
        Element students = root.element("students");
        students.elements("student").remove(3);
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/school.xml"), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}
